<template>
  <div class="broken-chart">
    <div class="title">{{ brokenTitle }}</div>
    <div class="chart" ref="brokenChartDom"></div>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, onMounted, toRefs, watch } from 'vue'
const brokenChartDom = ref(null)
const props = defineProps({
  brokenData: Array,
  brokenTitle: String
})
const { proxy } = getCurrentInstance()
const option = reactive({
  tooltip: {
    trigger: 'axis',
    backgroundColor: '#666',
    borderColor: '#666',
    textStyle: {
      color: '#fff'
    },
    axisPointer: {
      type: 'shadow'
    }
  },
  xAxis: {
    type: 'category',
    data: [],
    axisLabel: {
      show: true,
      color: '#ffffff'
    }
  },
  yAxis: {
    type: 'value',
    axisLabel: {
      show: true,
      color: '#ffffff'
    }
  },
  series: [
    {
      data: [],
      type: 'line',
      name: '次数'
    }
  ]
})
let brokenChart = null
const initChart = () => {
  brokenChart = proxy.$echarts.init(brokenChartDom.value)
  updateOption(option)
}
const updateOption = (option) => {
  const brokenXdata = reactive([])
  const brokenYdata = reactive([])
  const { brokenData } = toRefs(props)
  brokenData.value.forEach((item) => {
    brokenXdata.push(item.name)
    brokenYdata.push(item.value)
  })
  option.xAxis.data = brokenXdata
  option.series[0].data = brokenYdata
  brokenChart?.setOption(option)
}
watch(
  () => props.brokenData,
  () => {
    updateOption(option)
  }
)
onMounted(() => {
  initChart()
})
</script>

<style lang="less" scoped>
.broken-chart {
  width: 100%;
  height: 300px;
  color: #fff;
  background: url('@/assets/image/chart-bg.png') no-repeat;
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  .title {
    height: 40px;
    line-height: 40px;
    font-size: 18px;
    font-weight: 650;
    color: #01e4f8;
    text-align: center;
  }
  .chart {
    flex: 1;
  }
}
</style>
